package informationsystem

import org.springframework.dao.DataIntegrityViolationException

class UploadMapController {
	UploadMapService uploadMapService
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [uploadMapInstanceList: UploadMap.list(params), uploadMapInstanceTotal: UploadMap.count()]
    }

    def create() {
        [uploadMapInstance: new UploadMap(params)]
    }

    def save() {
        def uploadMapInstance = new UploadMap(params)
        if (!uploadMapInstance.save(flush: true)) {
            render(view: "create", model: [uploadMapInstance: uploadMapInstance])
            return
        }
		def shpFile = request.getFile('shpFile')
		if (!shpFile.isEmpty()) {
			uploadMapInstance.shpFile = uploadMapService.uploadFile(shpFile, "${uploadMapInstance.layerName}.shp", "ShapeFiles")
		}
		def dbfFile = request.getFile('dbfFile')
		if (!dbfFile.isEmpty()) {
			uploadMapInstance.dbfFile = uploadMapService.uploadFile(dbfFile, "${uploadMapInstance.layerName}.dbf", "ShapeFiles")
		}
		def prjFile = request.getFile('prjFile')
		if (!prjFile.isEmpty()) {
			uploadMapInstance.prjFile = uploadMapService.uploadFile(prjFile, "${uploadMapInstance.layerName}.prj", "ShapeFiles")
		}
		def shxFile = request.getFile('shxFile')
		if (!shxFile.isEmpty()) {
			uploadMapInstance.shxFile = uploadMapService.uploadFile(shxFile, "${uploadMapInstance.layerName}.shx", "ShapeFiles")
		}

		flash.message = message(code: 'default.created.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), uploadMapInstance.id])
        redirect(action: "show", id: uploadMapInstance.id)
    }

    def show() {
        def uploadMapInstance = UploadMap.get(params.id)
        if (!uploadMapInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), params.id])
            redirect(action: "list")
            return
        }

        [uploadMapInstance: uploadMapInstance]
    }

    def edit() {
        def uploadMapInstance = UploadMap.get(params.id)
        if (!uploadMapInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), params.id])
            redirect(action: "list")
            return
        }

        [uploadMapInstance: uploadMapInstance]
    }

    def update() {
        def uploadMapInstance = UploadMap.get(params.id)
        if (!uploadMapInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (uploadMapInstance.version > version) {
                uploadMapInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'uploadMap.label', default: 'UploadMap')] as Object[],
                          "Another user has updated this UploadMap while you were editing")
                render(view: "edit", model: [uploadMapInstance: uploadMapInstance])
                return
            }
        }

        uploadMapInstance.properties = params

        if (!uploadMapInstance.save(flush: true)) {
            render(view: "edit", model: [uploadMapInstance: uploadMapInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), uploadMapInstance.id])
        redirect(action: "show", id: uploadMapInstance.id)
    }

    def delete() {
        def uploadMapInstance = UploadMap.get(params.id)
        if (!uploadMapInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), params.id])
            redirect(action: "list")
            return
        }

        try {
            uploadMapInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'uploadMap.label', default: 'UploadMap'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
